package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author aiden
 * @date 2024/2/14 01:38
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        setmealDTO.getSetmealDishes().stream().forEach(e -> {
            e.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.saveInBatch(setmealDTO.getSetmealDishes());
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<Setmeal> page = setmealMapper.query(setmealPageQueryDTO);

        List<Category> list = categoryMapper.list(2);
        Map<Long, String> map = list.stream().collect(Collectors.toMap(Category::getId, Category::getName));

        List<SetmealVO> voList = page.getResult().stream().map(e -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(e, setmealVO);
            setmealVO.setCategoryName(map.get(setmealVO.getCategoryId()));
            return setmealVO;
        }).collect(Collectors.toList());

        return new PageResult(page.getTotal(), voList);
    }

    @Override
    public void updateStatus(String id, Integer status) {
        setmealMapper.updateStatus(id, status);
    }

    @Override
    public SetmealVO getById(String id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<Category> list = categoryMapper.list(2);
        Map<Long, String> map = list.stream().collect(Collectors.toMap(Category::getId, Category::getName));
        setmealVO.setCategoryName(map.get(setmealVO.getCategoryId()));

        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(Long.valueOf(id));
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(setmealDTO.getId());
        List<Long> collect = setmealDishes.stream().map(setmealDish -> setmealDish.getId()).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            setmealDishMapper.deleteBySetmealIdInBatch(collect);
        }

        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        list.stream().forEach(e->e.setSetmealId(setmeal.getId()));
        setmealDishMapper.saveInBatch(list);
    }

    @Override
    @Transactional
    public void deleteByIds(List<Integer> ids) {
        if (ids.isEmpty()) {
            throw new BaseException((MessageConstant.NULL_SETMEAL));
        }
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
